package com.csjy.migration.hnnd.workflow;

import com.csjy.migration.hnnd.DbUtil;
import com.csjy.migration.hnnd.ProgramArgument;
import com.csjy.migration.hnnd.Tool;
import com.csjy.migration.hnnd.annotation.Task;
import com.csjy.migration.hnnd.business.NdBpmFeeCollectionRecord;
import com.csjy.migration.hnnd.business.NdBpmResponCompensatory;
import com.csjy.migration.hnnd.business.SourceBusinessData;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 被动代偿申请
 */
public class ItemAskInstRefundHny extends WorkflowMigration {
    private NdBpmFeeCollectionRecord ndBpmFeeCollectionRecord = null;
    private NdBpmResponCompensatory ndBpmResponCompensatory = null;
    private Map<String,Object> lastBtItemChargeAudit = null;

    public ItemAskInstRefundHny(DbUtil sourceDbUtil, DbUtil targetDbUtil, SourceBusinessData sourceData, ProgramArgument programArgument, String projectId, String procInstId, Map procInstRow) throws Exception {
        super(sourceDbUtil, targetDbUtil, sourceData, programArgument, projectId, procInstId, procInstRow);
        customInitData();
    }

    @Override
    public void customInitData() throws Exception {
        this.curBizType = "bddc";
        this.ndBpmFeeCollectionRecord = new NdBpmFeeCollectionRecord(sourceDbUtil,targetDbUtil,sourceData,programArgument,projectId,procInstId,true);
        this.ndBpmResponCompensatory = new NdBpmResponCompensatory(sourceDbUtil,targetDbUtil,sourceData,programArgument,projectId,procInstId);
    }

    @Override
    public void customInsertOrUpdateData() throws Exception {
        ndBpmFeeCollectionRecord.start();
        ndBpmResponCompensatory.start();
    }

    @Task("被动代偿申请@项目主责任人提交申请")
    public void bddcsqxmzfzrtjsq() throws Exception{
        Optional<Map> btItemChargeAuditMap = this.sourceData.getBtItemChargeAuditList().stream()
            .filter(btItemChargeAudit->btItemChargeAudit.get("PROCID").toString().equals(this.procInstId) && btItemChargeAudit.get("TASKID").toString().equals(this.curJbpmTaskInstanceId))
            .findFirst();

        this.lastBtItemChargeAudit = btItemChargeAuditMap.isPresent() ? btItemChargeAuditMap.get() : null;

        this.curAffixType = "yhtz";//代偿方案
        this.curAffixTypeCommon = "qtfj";//其他附件

        this.curModelMap.put("all",new String[] {"key","同意"});

        this.curCustomData.put("coverageMoney",this.getJbpmVarInstValueByName("cassum"));//在保额
        this.curCustomData.put("guarantAgreeNo",this.sourceData.getBtItemBasic().get(0).get("ASSUCONTRACTNO"));//担保协议
        this.curCustomData.put("creditTimeStart",this.getJbpmVarInstValueByName("auditTime"));//授信开始时间
        this.curCustomData.put("creditTimeEnd",this.getJbpmVarInstValueByName("assumpTime"));//授信到期时间
        this.curCustomData.put("managerA",this.getJbpmVarInstValueByName("managerA"));//A经理
        this.curCustomData.put("managerB",this.getJbpmVarInstValueByName("managerB"));//B经理


//        this.curCustomData.put("compensatoryMoney",row.get("managerB"));//应付代偿合计,自动计算
        this.curCustomData.put("copyWithCompensatoryMoney",  lastBtItemChargeAudit == null ? null : lastBtItemChargeAudit.get("OUGHTSUM"));//应收代偿费用
        String favorMarkCount = this.getJbpmVarInstValueByName("favorMark-count");
        if(favorMarkCount == null || favorMarkCount.equals("")) {
            this.curCustomData.put("business",  null);// 输入框
        } else {
            Integer favorMarkCountInt = Integer.parseInt(favorMarkCount);
            String business = "";
            for(int i = 1;i<=favorMarkCountInt;i++) {
                business = business + Tool.replaceBrFlagStr(Tool.decodeUnicode(this.getJbpmVarInstValueByName("favorMark-"+i)));
            }
            this.curCustomData.put("business", business);
        }

        if(this.curCustomData.get("business") == null || this.curCustomData.get("business").toString().equals("")) {
            this.curCustomData.put("business",Tool.decodeUnicode(lastBtItemChargeAudit == null ? null : lastBtItemChargeAudit.get("FAVORMARK")));
        }

        if(this.curCustomData.get("business") == null || this.curCustomData.get("business").toString().equals("")) {
            this.curCustomData.put("business",Tool.decodeUnicode(this.getJbpmVarInstValueByName("favorMark")));
        }

        this.curModelMap.put("all",new String[] {"task1535443355164tg","通过"});
        this.setCommonData(true,null);

        this.ndBpmFeeCollectionRecord.getTaskParamMap().put(curJbpmTaskInstanceId,this.initTaskParamMap());
        this.ndBpmResponCompensatory.getTaskParamMap().put(curJbpmTaskInstanceId,this.initTaskParamMap());
    }

    @Task("被动代偿申请@部门负责人和风控经理审批")
    public void bddcsqbmfzrhfkjlsp() throws Exception {
        this.curModelMap.put("通过",new String[] {"task1587091347435tg","通过"});
        this.curModelMap.put("不通过",new String[] {"endbtg","不通过"});
        this.setCommonData(false,null);
    }

    @Task("被动代偿申请@风控副总审核")
    public void bddcsqfkfzsh() throws Exception {
        this.curModelMap.put("通过",new String[] {"task1563158143085tg","通过"});
        this.setCommonData(false,null);
    }

    @Task("被动代偿申请@财务部经理审核")
    public void bddcsqcwbjlsh() throws Exception {
        this.curModelMap.put("通过",new String[] {"task1563158144756tg","通过"});
        this.setCommonData(false,null);
    }

    @Task("被动代偿申请@财务副总审核")
    public void bddcsqcwfzsh() throws Exception {
        this.curModelMap.put("通过",new String[] {"task1535443749106tg","通过"});
        this.setCommonData(false,null);
    }

    @Task("被动代偿申请@业务副总审核")
    public void bddcsqywfzsh() throws Exception {
        this.curModelMap.put("通过",new String[] {"task1563158146483tg","通过"});
        this.setCommonData(false,null);
    }

    @Task("被动代偿申请@总经理审批")
    public void bddcsqzjlsp() throws Exception {
        this.curModelMap.put("通过",new String[] {"task1535443717445tg","通过"});
        this.curModelMap.put("不通过",new String[] {"endbtg","不通过"});
        this.setCommonData(false,null);
    }

    @Task("被动代偿申请@董事长审批")
    public void bddcsqdszsp() throws Exception {
        this.curModelMap.put("通过",new String[] {"task1535443680769tg","通过"});
        this.curModelMap.put("不通过",new String[] {"endbtg","不通过"});
        this.setCommonData(false,null);
    }

    @Task("被动代偿申请@财务部执行代偿")
    public void bddcsqcwbzxdc() throws Exception{

        this.curAffixType = "dchd";
        this.curModelMap.put("已执行",new String[] {"endyzx","同意"});
        this.curModelMap.put("驳回",new String[] {"驳回","驳回"});

        this.curCustomData.put("copyWithCompensatoryMoney", lastBtItemChargeAudit == null ? null : this.lastBtItemChargeAudit.get("DEALSUM"));// 实收客户代偿服务费

        this.setCommonData(false,null);


        this.ndBpmResponCompensatory.getTaskParamMap().put(curJbpmTaskInstanceId,this.initTaskParamMap());

    }

    @Task("代偿申请流程@项目主责任人提交申请")
    public void dcsqlcxmzfzrtjsq() throws Exception{
        bddcsqxmzfzrtjsq();
    }

    @Task("代偿申请流程@部门负责人和风控经理审批")
    public void  dcsqlcbmfzrhfkjlsp() throws Exception {
        bddcsqbmfzrhfkjlsp();
    }

    @Task("代偿申请流程@风控副总审核")
    public void dcsqlcfkfzsh() throws Exception {
        bddcsqfkfzsh();
    }

    @Task("代偿申请流程@风控副总审批")
    public void dcsqlcfkfzsp() throws Exception {
        bddcsqfkfzsh();
    }

    @Task("代偿申请流程@财务部经理审核")
    public void dcsqlccwbjlsh() throws Exception {
        bddcsqcwbjlsh();
    }

    @Task("代偿申请流程@财务副总审核")
    public void dcsqlccwfzsh() throws Exception {
        bddcsqcwfzsh();
    }

    @Task("代偿申请流程@业务副总审核")
    public void csqlcyfwzsh() throws Exception {
        bddcsqywfzsh();
    }

    @Task("代偿申请流程@总经理审批")
    public void dcsqlczjlsp() throws Exception {
        bddcsqzjlsp();
    }

    @Task("代偿申请流程@董事长审批")
    public void dcsqlcdszsp() throws Exception {
        bddcsqdszsp();
    }

    @Task("代偿申请流程@财务部执行代偿")
    public void dcsqlccwbzxdc() throws Exception{
        bddcsqcwbzxdc();
    }
}
